namespace QMVnSoft.SystemForms
{
    using QMVnSoft;
    using QMVnSoft.Controls;
    using QMVnSoft.DAL;
    using System;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Windows.Forms;

    public class FormCompanyList : FormControl
    {
        private LViewControl CompanyList;
        private IContainer components;
        private ColumnHeader Header1;
        private ColumnHeader Header2;
        private bool ib_NoEscaple;

        public FormCompanyList()
        {
            this.InitializeComponent();
            this.GenerateItems();
        }

        public FormCompanyList(bool pb_NoEscape)
        {
            this.InitializeComponent();
            this.GenerateItems();
            this.ib_NoEscaple = pb_NoEscape;
        }

        private void CompanyList_ItemActivate(object sender, EventArgs e)
        {
            AppInstance.CompanyRegister(this.CompanyList.Items[this.CompanyList.SelectedIndices[0]].SubItems[2].Text);
            base.Close();
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing && (this.components != null))
            {
                this.components.Dispose();
            }
            base.Dispose(disposing);
        }

        private void GenerateItems()
        {
            DataTable companies = new CompanyDataAccess().GetCompanies();
            this.CompanyList.Items.Clear();
            int num = 0;
            foreach (DataRow row in companies.Rows)
            {
                if (((string) row["Ma_Dvcs"]) == AppInstance.Subsidiary)
                {
                    this.CompanyList.Items.Add((string) row["Ten_Dvcs"]);
                    DateTime time = (DateTime) row["Ngay_Bd_Ht"];
                    this.CompanyList.Items[num].SubItems.Add(time.ToShortDateString());
                    this.CompanyList.Items[num].SubItems.Add((string) row["Ma_Dvcs"]);
                    this.CompanyList.Items[num].Group = this.CompanyList.Groups[0];
                    num++;
                }
            }
            foreach (DataRow row2 in companies.Rows)
            {
                if (((string) row2["Ma_Dvcs"]) != AppInstance.Subsidiary)
                {
                    this.CompanyList.Items.Add((string) row2["Ten_Dvcs"]);
                    DateTime time2 = (DateTime) row2["Ngay_Bd_Ht"];
                    this.CompanyList.Items[num].SubItems.Add(time2.ToShortDateString());
                    this.CompanyList.Items[num].SubItems.Add((string) row2["Ma_Dvcs"]);
                    this.CompanyList.Items[num].Group = this.CompanyList.Groups[1];
                    num++;
                }
            }
            this.CompanyList.Items[0].Selected = true;
        }

        private void InitializeComponent()
        {
            System.Windows.Forms.ListViewGroup listViewGroup1 = new System.Windows.Forms.ListViewGroup("Đang làm việc với", System.Windows.Forms.HorizontalAlignment.Left);
            System.Windows.Forms.ListViewGroup listViewGroup2 = new System.Windows.Forms.ListViewGroup("Các đơn vị còn lại", System.Windows.Forms.HorizontalAlignment.Left);
            System.Windows.Forms.ListViewItem listViewItem1 = new System.Windows.Forms.ListViewItem(new string[] {
            "Công ty TNHH phần mềm Quang Minh",
            "01/01/2009"}, -1);
            System.Windows.Forms.ListViewItem listViewItem2 = new System.Windows.Forms.ListViewItem(new string[] {
            "Công ty Kiểm toán Hoàng Cầu",
            "01/01/2009"}, -1);
            System.Windows.Forms.ListViewItem listViewItem3 = new System.Windows.Forms.ListViewItem(new string[] {
            "Công ty thương mại Hoàng Gia",
            "01/01/2009"}, -1);
            this.CompanyList = new QMVnSoft.Controls.LViewControl();
            this.Header1 = new System.Windows.Forms.ColumnHeader();
            this.Header2 = new System.Windows.Forms.ColumnHeader();
            this.SuspendLayout();
            // 
            // CompanyList
            // 
            this.CompanyList.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
                        | System.Windows.Forms.AnchorStyles.Left)
                        | System.Windows.Forms.AnchorStyles.Right)));
            this.CompanyList.Columns.AddRange(new System.Windows.Forms.ColumnHeader[] {
            this.Header1,
            this.Header2});
            this.CompanyList.Font = new System.Drawing.Font("Arial", 8.25F);
            this.CompanyList.FullRowSelect = true;
            listViewGroup1.Header = "Đang làm việc với";
            listViewGroup1.Name = "GroupItem01";
            listViewGroup2.Header = "Các đơn vị còn lại";
            listViewGroup2.Name = "GroupItem02";
            this.CompanyList.Groups.AddRange(new System.Windows.Forms.ListViewGroup[] {
            listViewGroup1,
            listViewGroup2});
            this.CompanyList.HeaderStyle = System.Windows.Forms.ColumnHeaderStyle.None;
            listViewItem1.Group = listViewGroup1;
            listViewItem1.StateImageIndex = 0;
            listViewItem2.Group = listViewGroup2;
            listViewItem2.StateImageIndex = 0;
            listViewItem3.Group = listViewGroup2;
            listViewItem3.StateImageIndex = 0;
            this.CompanyList.Items.AddRange(new System.Windows.Forms.ListViewItem[] {
            listViewItem1,
            listViewItem2,
            listViewItem3});
            this.CompanyList.Location = new System.Drawing.Point(12, 12);
            this.CompanyList.MultiSelect = false;
            this.CompanyList.Name = "CompanyList";
            this.CompanyList.Size = new System.Drawing.Size(427, 209);
            this.CompanyList.TabIndex = 0;
            this.CompanyList.UseCompatibleStateImageBehavior = false;
            this.CompanyList.View = System.Windows.Forms.View.Details;
            this.CompanyList.ItemActivate += new System.EventHandler(this.CompanyList_ItemActivate);
            // 
            // Header1
            // 
            this.Header1.Text = "Tên đơn vị";
            this.Header1.Width = 327;
            // 
            // Header2
            // 
            this.Header2.Text = "Ngày bắt đầu";
            this.Header2.TextAlign = System.Windows.Forms.HorizontalAlignment.Center;
            this.Header2.Width = 79;
            // 
            // FormCompanyList
            // 
            this.ClientSize = new System.Drawing.Size(451, 233);
            this.Controls.Add(this.CompanyList);
            this.MaximizeBox = false;
            this.MinimizeBox = false;
            this.Name = "FormCompanyList";
            this.ShowInTaskbar = false;
            this.Text = "Danh mục đơn vị cơ sở: F3-Sửa";
            this.ResumeLayout(false);

        }

        protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {
            if (keyData == Keys.F3)
            {
                FormCompanyEditor editor = new FormCompanyEditor();
                editor.CompanyCode = this.CompanyList.Items[this.CompanyList.SelectedIndices[0]].SubItems[2].Text;
                editor.ShowDialog();
                if (editor.CheckAccepted)
                {
                    this.CompanyList.Items[this.CompanyList.SelectedIndices[0]].SubItems[0].Text = editor.CompanyFullName;
                    this.CompanyList.Items[this.CompanyList.SelectedIndices[0]].SubItems[1].Text = editor.StartDate;
                }
                return true;
            }
            if (keyData == Keys.Return)
            {
                AppInstance.CompanyRegister(this.CompanyList.Items[this.CompanyList.SelectedIndices[0]].SubItems[2].Text);
                base.Close();
                return true;
            }
            if ((keyData == Keys.Escape) && !this.ib_NoEscaple)
            {
                base.Close();
                return true;
            }
            return base.ProcessCmdKey(ref msg, keyData);
        }
    }
}

